package util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Trabalho PD - 2014/2015
 *
 * @author a21210380 a21210392
 */
public class Validator {

    public static Integer parseInt(String s) {
        if (ValidNumber.isInteger(s)) {
            try {
                return Integer.parseInt(s);
            } catch (NumberFormatException e) {
                return null;
            }
        }
        return null;
    }

    public static Long parseLong(String s) {
        if (ValidNumber.isInteger(s)) {
            try {
                return Long.parseLong(s);
            } catch (NumberFormatException e) {
                return null;
            }
        }
        return null;
    }

    public static Float parseFloat(String s) {
        if (ValidNumber.isFloat(s)) {
            try {
                return Float.parseFloat(s);
            } catch (NumberFormatException e) {
                return null;
            }
        }
        return null;
    }

    public static Double parseDouble(String s) {
        if (ValidNumber.isFloat(s)) {
            try {
                return Double.parseDouble(s);
            } catch (NumberFormatException e) {
                return null;
            }
        }
        return null;
    }

    public static Boolean parseBoolean(String s) {
        switch (s) {
            case "1":
                return true;
            case "0":
                return false;
            case "true":
                return true;
            case "false":
                return false;
            case "TRUE":
                return true;
            case "FALSE":
                return false;
            case "True":
                return true;
            case "False":
                return false;
        }
        return null;
    }

    public static Date parseDate(String s) {
        try {
            return new SimpleDateFormat("MM-dd-yy HH:mm:ss").parse(s);
        } catch (ParseException ex) {
            try {
                return new SimpleDateFormat("yyyy/MM/dd HH:mm").parse(s);
            } catch (ParseException ex1) {
                return null;
            }
        }
    }
    
    public static Date parseDate(long date) {
        if(date < 0) {
            return null;
        }
        return new Date(date);
    }
    
    public static Date tryParseDate(String s) {
        try {
            return new SimpleDateFormat().parse(s);
        } catch (ParseException ex) {
            return null;
        }
    }

}
